﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.GamerServices;

namespace Antioch
{
    class ScreenPressStart : Screen
    {
		Texture2D Title;
		SpriteFont Font;

        public override void Initialize()
        {
			Title = Content.Load<Texture2D>("Antioch.Content/title");
			Font = Content.Load<SpriteFont>("Antioch.Content/hud");
        }

        public override void ButtonEvent(PlayerIndex index, Base.EventBasedInput.Buttons button, Base.EventBasedInput.EventTypes type, TimeSpan duration) 
        {
            if (button == Base.EventBasedInput.Buttons.Start && type == Base.EventBasedInput.EventTypes.Pressed)
            {
				MainController = index;

                EasyStorage.PlayerSaveDevice playerSaveDevice = 
                new EasyStorage.PlayerSaveDevice(MainController);
			Screen.Game.Components.Add(playerSaveDevice);

			// hook an event for when the device is selected
			//playerSaveDevice.DeviceSelected += (s, e) => Ready = true;
			
			// hook two event handlers to force the user to choose a new device if they cancel the
			// device selector or if they disconnect the storage device after selecting it
			playerSaveDevice.DeviceSelectorCanceled += (s, e) => e.Response = EasyStorage.SaveDeviceEventResponse.Force;
			playerSaveDevice.DeviceDisconnected += (s, e) => e.Response = EasyStorage.SaveDeviceEventResponse.Force;

			// prompt for a device on the next Update
			playerSaveDevice.PromptForDevice();

			// make sure we hold on to the device
			Screen.SaveDevice = playerSaveDevice;
			}
		}

		public override void Update(GameTime gameTime)
        {
            if (SaveDevice != null && SaveDevice.IsReady)
			{
				PopScreen();
				PushScreen(new ScreenGame());
			}
        }

        public override void Draw()
        {
			GraphicsDevice.Clear(Color.Black);
			int ScreenWidth = GraphicsDevice.Viewport.TitleSafeArea.Width;
			int ScreenHeight = GraphicsDevice.Viewport.TitleSafeArea.Height;
			Rectangle SpritePosition = new Rectangle();
			SpritePosition.X = GraphicsDevice.Viewport.TitleSafeArea.X + (ScreenWidth / 2) - ScreenHeight;
			SpritePosition.Width = ScreenHeight * 2;
			SpritePosition.Y = GraphicsDevice.Viewport.TitleSafeArea.Y;
			SpritePosition.Height = ScreenHeight;


			SpriteBatch.Begin();

			SpriteBatch.Draw(Title, SpritePosition, Color.White);


            SpriteBatch.DrawString(Font, "Press Start", new Vector2(256, 256), Color.White);
            SpriteBatch.End();
        }
    }
}
